package com.jd.oneday.month3;
/**
 * 1.单链表的反转
 * 2.从尾到头打印单链表
 */

import java.util.Stack;

public class Day08 {

    public static void main(String[] args) {

        LinkedListNode4 linkedListNodes1 = new LinkedListNode4(78, "刘伟超", 5);
        LinkedListNode4 linkedListNodes2 = new LinkedListNode4(85, "刘广超", 4);
        LinkedListNode4 linkedListNodes3 = new LinkedListNode4(58, "刘满超", 14);
        LinkedListNode4 linkedListNodes4 = new LinkedListNode4(45, "刘宇鹏", 7);

        ManageLinkedList1 manageLinkedList1 = new ManageLinkedList1();
        manageLinkedList1.add(linkedListNodes1);
        manageLinkedList1.add(linkedListNodes2);
        manageLinkedList1.add(linkedListNodes3);
        manageLinkedList1.add(linkedListNodes4);

//        System.out.println("反转链表前");
//        manageLinkedList1.list();
//
//        System.out.println("反转链表后");
//        manageLinkedList1.reverseList(manageLinkedList1.getNode());
//        manageLinkedList1.list();

        manageLinkedList1.reversePrint(manageLinkedList1.getNode());

    }


}


class ManageLinkedList1 {
    private LinkedListNode4 node = new LinkedListNode4(0, "", 0);


    public LinkedListNode4 getNode() {
        return node;
    }

    //创建单链表
    public void add(LinkedListNode4 n) {
        LinkedListNode4 temp = node;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = n;
    }


    //从尾到头打印单链表
    public void reversePrint(LinkedListNode4 head) {
        isNull();

        LinkedListNode4 temp = head.next;
        Stack<LinkedListNode4> stack = new Stack<>();

        while (temp != null) {  //判断是否到达链表最后一个节点
            stack.push(temp);  //将每个节点压入栈中
            temp = temp.next;   //链表节点后移
        }

        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }
    }


    //反转链表
    public void reverseList(LinkedListNode4 head) {
        //如果链表为空或者只有一个节点   无需反转
        if (head.next == null || head.next.next == null) {
            return;
        }

        LinkedListNode4 reverseHead = new LinkedListNode4(0, "0", 0);  //定义一个新的链表头
        LinkedListNode4 temp = head.next;
        LinkedListNode4 next = null;   //指向当前节点的下一个节点

        while (temp != null) {

            next = temp.next;   //将节点先保存下来
            temp.next = reverseHead.next;   //将新链表节点的指向移到 新加入最新节点 的指向
            reverseHead.next = temp;  //将temp节点链接到新链表上
            temp = next;  //链表节点后移

        }
        //将reverseHead指向node
        node.next = reverseHead.next;
    }

    //遍历链表
    public void list() {
        isNull();

        LinkedListNode4 temp = node;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
            System.out.println(temp);
        }
    }

    //判断链表是否为空
    public void isNull() {
        if (node.next == null) {
            System.out.println("这是一个空链表");
            return;
        }
    }
}


class LinkedListNode4 {

    int id;
    String name;
    int age;
    LinkedListNode4 next;

    public LinkedListNode4(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "LinkedListNode3{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}




